package com.etekcity.vesync.common.plus.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.ImageButton;

import com.etekcity.vesync.common.plus.R;

/**
 * @author liaoheng
 * @version 2015年9月22日
 */
public class ToggleImageButton extends ImageButton implements Checkable {
    private OnCheckedChangeListener mOnCheckedChangeListener;
    private boolean async;
    private boolean asyncSelect;

    public ToggleImageButton(Context context) {
        super(context);
    }

    public ToggleImageButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ToggleImageButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ToggleImageButton(Context context,
                             AttributeSet attrs,
                             int defStyleAttr,
                             int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    public void init(Context context, AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
        TypedArray a = null;
        try {
            a = context.obtainStyledAttributes(attrs, R.styleable.ToggleImageButton);
            this.async = a.getBoolean(R.styleable.ToggleImageButton_isAsync, false);
        } finally {
            if (a != null) {
                a.recycle();
            }
        }

    }

    @Override
    public boolean isChecked() {
        return isSelected();
    }

    public boolean isAsyncSelect() {
        return asyncSelect;
    }

    @Override
    public void setSelected(boolean selected) {
        if (async) {
            asyncSelect = selected;
        }
        super.setSelected(selected);
    }

    @Override
    public void setChecked(boolean checked) {
        if (async) {
            asyncSelect = checked;
        } else {
            setSelected(checked);
            if (mOnCheckedChangeListener != null) {
                mOnCheckedChangeListener.onCheckedChanged(this, checked);
            }
        }
    }

    public void toggleSelected() {
        setSelected(!isSelected());
    }

    @Override
    public void toggle() {
        setChecked(!isChecked());
    }

    @Override
    public boolean performClick() {
        toggle();
        return super.performClick();
    }

    /**
     * 之后使用setSelected 来标记状态
     */
    public void enableAsync() {
        this.async = true;
    }

    public void unableAsync() {
        this.async = false;
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.mOnCheckedChangeListener = onCheckedChangeListener;
    }

    public interface OnCheckedChangeListener {
        void onCheckedChanged(ToggleImageButton buttonView, boolean isChecked);
    }
}
